<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
             label-position="left">

      <div class="title-container">
        <h3 class="title">驾驶员管理后台</h3>
      </div>

      <el-form-item prop="name">
        <el-input prefix-icon="el-icon-user" v-model="loginForm.name" placeholder="用户名" name="name"
                  type="text"
                  tabindex="1" auto-complete="on"/>
      </el-form-item>

      <el-form-item prop="psw">
        <el-input prefix-icon="el-icon-lock" v-model="loginForm.psw"
                  type="password"
                  placeholder="密码" name="psw" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin"/>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;"
                 @click.native.prevent="handleLogin">登录
      </el-button>

    </el-form>
  </div>
</template>

<script>
import md5 from 'crypto-js/md5';
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('请输入正确的密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        name: 'admin',
        psw: '123'
      },
      loginRules: {
        name: [{required: true, trigger: 'blur', validator: validateUsername}],
        psw: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      //验证表单元素是否符合规则
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          let params = {
            ...this.loginForm
          }
          this.$store.dispatch('user/login', params).then(() => {
            this.$router.push({path: this.redirect || '/'})
            this.loading = false
            this.$message.success("登录成功");
          }).catch((e) => {
            console.log(e)
            this.$message.error(e);
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #333;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;

  .login-form {
    position: absolute;
    width: 400px;
    padding: 30px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(255, 255, 255, .2);
    overflow: hidden;
    box-shadow: 0 2px 3px 2px rgba(0, 0, 0, 0.1);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 0px 5px 0px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 24px;
      color: $light_gray;
      margin: 0 0 30px 0;
      font-weight: 300;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
